package com.novax.ex.ai.test;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import lombok.Data;

public class GPT3Example {

    private static final String GPT_URL = "https://api.openai.com/v1/engines/text-davinci-003/completions";
    private static final String API_KEY = "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";

    private static final String BODY = "%s。\"提取前一句中的主要谓语和宾语，并以JSON的格式返回。例如：我想买BTC。这个句子的主干是 \\\"买\\\" 和 \\\"BTC\\\"，请将它们放入JSON 中，格式为 {\\\"action\\\":\\\"buy\\\",\\\"object\\\":\\\"BTC\\\"}。\"";

    public static void main(String[] args) {


        System.setProperty("http.proxyHost", "127.0.0.1");
        System.setProperty("https.proxyHost", "127.0.0.1");
        System.setProperty("http.proxyPort", "7890");
        System.setProperty("https.proxyPort", "7890");

        long start = System.currentTimeMillis();

        RequestBody requestBody = new RequestBody();
//        String content = String.format(BODY, "我想买BTC");
        String content = String.format(BODY, "我想买pipe");
        requestBody.setPrompt("请返回你最大能返回字数的文章，文章内容随意");
        requestBody.setMax_tokens(5000);

        while (true) {
            HttpResponse response = HttpRequest.post(GPT_URL)
                    .header("Content-Type", "application/json")
                    .header("Authorization", "Bearer " + API_KEY)
                    .body(JSON.toJSONString(requestBody))
                    .execute();
        }

//        System.out.println(response.body());
//
//        System.out.println("请求总耗时：" + (System.currentTimeMillis() - start));

    }

    @Data
    private static class RequestBody {
        private String prompt;
        private Integer max_tokens;
    }
}
